﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace FromCambia
{
    //https://www.cambiaresearch.com/articles/33/how-can-i-easily-manage-an-xml-configuration-file-in-dotnet
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Configuration configuration = new Configuration();
            configuration.StringItem = "My Config";
            configuration.IntItem = 2456;

            Configuration.Serialize("simple.config", configuration);

            Configuration configuration2 = Configuration.Deserialize("simple.config");

            Console.WriteLine(configuration2.StringItem);
            Console.WriteLine(configuration2.IntItem);
        }
    }

    [Serializable]
    public class Configuration
    {
        int _Version, _IntItem;
        String _StringItem;

        public Configuration()
        {
            _Version = 1;
            _StringItem = "";
            _IntItem = -1;
        }

        public static void Serialize(string file,Configuration c)
        {
            XmlSerializer xs = new XmlSerializer(c.GetType());
            StreamWriter writer = File.CreateText(file);
            xs.Serialize(writer, c);
            writer.Flush();
            writer.Close();
        }

        public static Configuration Deserialize(string file)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Configuration));
            StreamReader reader = File.OpenText(file);
            Configuration c = (Configuration)xs.Deserialize(reader);
            reader.Close();
            return c;
        }

        public int Version { get => _Version; set => _Version = value; }
        public int IntItem { get => _IntItem; set => _IntItem = value; }
        public string StringItem { get => _StringItem; set => _StringItem = value; }
    }
}
